/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.tnmk.worktime.service.dao;

import java.util.Collection;
import java.util.List;
import org.apache.log4j.Logger;
import org.tnmk.worktime.service.dao.entry.BaseEntry;

/**
 *
 * @author tnmkhoi
 * @param <E> extends from BaseEntry, which has attribute 'id' and serializable
 * (for storing to file)
 */
public class BaseDao<E extends BaseEntry> {

  private static final Logger LOG = Logger.getLogger(BaseDao.class);
  private StorageBuffer storageBuffer = DailyRecordBuffer.getInstance();
  private Class<E> entryClass;

  protected Collection<E> getAll() {
    DataTable<E> data = storageBuffer.getStorage().get(entryClass);
    return data.values();
  }

  public BaseDao(Class<E> entryClass) {
    this.entryClass = entryClass;
//    ioService = IoService.getInstance();
//    all = this.loadAll();
  }

  public void backupData() {
    this.storageBuffer.backupData();
  }

  public void delete(Integer id) {
    storageBuffer.delete(this.entryClass, id);
//    E e = find(id);
//    this.delete(e);
  }

  public boolean addOrUpdate(E e) {
    return storageBuffer.addOrUpdate(e);
//    return save();
  }

  public boolean addOrUpdate(List<E> list) {
    return storageBuffer.addOrUpdate(list);
  }

  /**
   * @param list this list will replace the current data
   * @return
   */
  public boolean save() {
    return storageBuffer.save();
  }

  public E find(Integer id) {
    return storageBuffer.find(entryClass, id);
  }

  public List<E> findAll() {
    return storageBuffer.findAll(entryClass);
  }

  protected Class<E> getEntryClass() {
    return entryClass;
  }
}
